//
//  DanZiXunLianViewController.swift
//  ZHM
//
//  Created by zzddongge on 16/7/22.
//  Copyright © 2016年 zzd. All rights reserved.
//

import UIKit

class DanZiXunLianViewController: UIViewController,DanZiSheZhiViewControllerDelegate {

    let screenWidth = UIScreen.main.bounds.width
    let screenHeight = UIScreen.main.bounds.height
    
    var navigationHeight:CGFloat?
    
    //创建笔形训练说明,让读者一目了然
    var danZiXunLianDescription:UILabel?
    
    //定义训练时间UILabel对象
    var xunLianShiJian:UILabel?
    //定义训练分钟值UILabel对象
    var xunLianShiJianZhi:UILabel?
    //定义分钟UILabel对象
    var fenZhong:UILabel?
    
    //定义训练模式UILabel对象
    var xunLianMoShi:UILabel?
    //定义训练模式值UILabel对象
    var xunLianMoShiZhi:UILabel?
    //定义模式UILabel对象
    var moShi:UILabel?
    
    //定义选项按钮
    var xuanXiangBtn:UIButton?
    //定义开始按钮
    var kaiShiBtn:UIButton?

    override func viewDidLoad() {
        fengZhuangViewDidLoad()
        // Do any additional setup after loading the view.
    }

    /*
     作者:张振东
     日期:20176-09-24
     功能:封装viewDidLoad函数中的初始化label对象
     */
    func fengZhuangUiLabelAttributes(_ label:UILabel){
        switch label {
        case danZiXunLianDescription!:
            //初始化小键盘训练说明
            label.layer.cornerRadius = 10
            label.clipsToBounds = true
            label.backgroundColor = UIColor(red: 232/255.0,green:232/255.0,blue:232/255.0,alpha:1)
            label.text = "单字训练中,要训练的单字用灰色标出,首次输入错误输入框将变为红色以示警告.点击背景图片,您可以查看该单字对应的纵横码,单击'开始'按钮开始训练,两次之内输入正确,将自动生成新的图片"
            label.font = UIFont.systemFont(ofSize: 17)
            label.layer.borderWidth = 1
            label.layer.borderColor = UIColor(red: 232/255.0,green:232/255.0,blue:232/255.0,alpha:1).cgColor
            label.numberOfLines = 0
            self.automaticallyAdjustsScrollViewInsets = false
        case xunLianShiJian!:
            label.text = "训练时间:"
            label.font = UIFont.systemFont(ofSize: 16)
            
        case xunLianShiJianZhi!:
            label.textAlignment = .center
            label.font = UIFont.systemFont(ofSize: 16)
            label.text = "1"
        case fenZhong!:
            
            label.text = "分钟"
            label.font = UIFont.systemFont(ofSize: 16)
        case xunLianMoShi!:
            
            label.font = UIFont.systemFont(ofSize: 16)
            label.text = "训练模式:"
        case xunLianMoShiZhi!:
            label.textAlignment = .center
            label.font = UIFont.systemFont(ofSize: 16)
            label.text = "计时"
        case moShi!:
            label.font = UIFont.systemFont(ofSize: 16)
            label.text = "模式"
        default:
            print("初始化完毕")
        }
        
    }

    
    func fengZhuangViewDidLoad(){
        self.title = "单字训练"
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont(name: "STHeitiSC-Medium", size: 20.0)!]
        self.view.backgroundColor = UIColor(red: 246,green: 246,blue: 246,alpha: 1)
        let leftBarButton = UIButton(frame:CGRect(x: 0,y: 0,width: 20,height: 20))
        leftBarButton.setBackgroundImage(UIImage(named: "fanhui"), for: UIControlState())
        leftBarButton.addTarget(self, action: #selector(backToPrevious), for: .touchUpInside)
        let leftBarButtonItem = UIBarButtonItem(customView: leftBarButton)
        self.navigationItem.leftBarButtonItems = [leftBarButtonItem]
        
        
        //获取导航栏的高度
        navigationHeight = self.navigationController?.navigationBar.bounds.size.height
        
        //初始化小键盘训练说明
        danZiXunLianDescription = UILabel(frame:CGRect(x: 10,y: navigationHeight!+70,width: screenWidth-20,height: 100))
        fengZhuangUiLabelAttributes(danZiXunLianDescription!)
        //初始化训练时间UILabel对象
        xunLianShiJian = UILabel(frame:CGRect(x: screenWidth/2-70,y: navigationHeight!+70+100+50,width: 70,height: 25))
        fengZhuangUiLabelAttributes(xunLianShiJian!)
        
        //初始化训练时间值UILabel对象
        xunLianShiJianZhi = UILabel(frame:CGRect(x: screenWidth/2+15,y: navigationHeight!+70+100+50,width: 30,height: 25))
        fengZhuangUiLabelAttributes(xunLianShiJianZhi!)
        
        //初始化分钟UILabel对象
        fenZhong = UILabel(frame:CGRect(x: screenWidth/2+50,y: navigationHeight!+70+100+50,width: 40,height: 25))
        fengZhuangUiLabelAttributes(fenZhong!)
        
        
        //初始化训练模式UIlabel对象
        xunLianMoShi = UILabel(frame:CGRect(x: screenWidth/2-70,y: navigationHeight!+70+100+50+25+20,width: 70,height: 25))
        fengZhuangUiLabelAttributes(xunLianMoShi!)
        
        //初始化训练模式值UILabel对象
        xunLianMoShiZhi = UILabel(frame:CGRect(x: screenWidth/2+10,y: navigationHeight!+70+100+50+25+20,width: 40,height: 25))
        fengZhuangUiLabelAttributes(xunLianMoShiZhi!)
        
        //初始化训练模式值UILabel对象
        moShi = UILabel(frame:CGRect(x: screenWidth/2+50,y: navigationHeight!+70+100+50+25+20,width: 40,height: 25))
        fengZhuangUiLabelAttributes(moShi!)
        
        //初始化我知道了按钮
        kaiShiBtn = UIButton(frame:CGRect(x: 10,y: screenHeight-60,width: screenWidth-20,height: 40))
        kaiShiBtn?.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
        kaiShiBtn?.setTitle("开始", for: UIControlState())
        kaiShiBtn?.layer.cornerRadius = 10
        kaiShiBtn?.setTitleColor(UIColor.white, for: UIControlState())
        
        //初始化选项按钮
        xuanXiangBtn = UIButton(frame:CGRect(x: 10,y: screenHeight-110,width: screenWidth-20,height: 40))
        xuanXiangBtn?.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
        xuanXiangBtn?.setTitle("选项", for: UIControlState())
        xuanXiangBtn?.layer.cornerRadius = 10
        xuanXiangBtn?.setTitleColor(UIColor.white, for: UIControlState())
        
        self.view.addSubview(danZiXunLianDescription!)
        self.view.addSubview(xunLianShiJian!)
        self.view.addSubview(xunLianShiJianZhi!)
        self.view.addSubview(fenZhong!)
        self.view.addSubview(xunLianMoShi!)
        self.view.addSubview(xunLianMoShiZhi!)
        self.view.addSubview(moShi!)
        self.view.addSubview(xuanXiangBtn!)
        self.view.addSubview(kaiShiBtn!)
        
        //给按钮"我知道了"添加点击事件
        kaiShiBtn?.addTarget(self, action: #selector(tiaoZhuanZhiKaiShiXunLianViewController), for: .touchUpInside)
        xuanXiangBtn?.addTarget(self, action: #selector(tiaoZhuanZhiXuanXiangViewController), for: .touchUpInside)
        super.viewDidLoad()

    }
    
    override func viewWillAppear(_ animated: Bool) {
        self.title = "单字训练"
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont(name: "STHeitiSC-Medium", size: 20.0)!]
    }

    
    func chuanDiDanZiMoShiAndFenZhong(_ xuanZhongDeMoShiZhi: String, xuanZhongFenZhongZhi: Int) {
        print(xuanZhongDeMoShiZhi)
        print(xuanZhongFenZhongZhi)
        self.xunLianMoShiZhi?.text = xuanZhongDeMoShiZhi
        self.xunLianShiJianZhi?.text = "\(xuanZhongFenZhongZhi)"
        
    }

    func tiaoZhuanZhiXuanXiangViewController(){
        let danZiXuanXiangViewController = DanZiXuanXiangViewController()
        danZiXuanXiangViewController.delegate = self
        self.navigationController?.pushViewController(danZiXuanXiangViewController, animated:true)

    }
    
    func tiaoZhuanZhiKaiShiXunLianViewController(){
//        let danZiXunLianMainViewController = DanZiXunLianMainViewController()
//        danZiXunLianMainViewController.moShiLeiBie = self.xunLianMoShiZhi?.text
//        danZiXunLianMainViewController.fenZhongZhi = self.xunLianShiJianZhi?.text
//        self.navigationController?.pushViewController(danZiXunLianMainViewController, animated: true)
        
        let danZiXunLianExersizeViewController = DanZiXunLianExersizeViewController()
        danZiXunLianExersizeViewController.moShiLeiBie = self.xunLianMoShiZhi?.text
        danZiXunLianExersizeViewController.fenZhongZhi = self.xunLianShiJianZhi?.text
        self.navigationController?.pushViewController(danZiXunLianExersizeViewController, animated: true)
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func backToPrevious(){
        self.navigationController?.popViewController(animated: true)
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
